package com.ruoyi.mes.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.LnyStoreroomMapper;
import com.ruoyi.mes.mapper.WkspMaterialListDetailMapper;
import com.ruoyi.mes.mapper.WkspMaterialListMapper;
import com.ruoyi.mes.utils.CodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.LnyStorageOutMapper;
import com.ruoyi.mes.service.ILnyStorageOutService;
import com.ruoyi.mes.mapper.LnyProInventoryMapper;
import com.ruoyi.mes.mapper.LnyOutOrderMapper;

/**
 * 出库管理Service业务层处理
 * 
 * @author CFL
 * @date 2024-04-28
 */
@Service
public class LnyStorageOutServiceImpl implements ILnyStorageOutService 
{
    @Autowired
    private LnyStorageOutMapper lnyStorageOutMapper;

    @Autowired
    private LnyOutOrderMapper lnyOutOrderMapper;

    @Autowired
    private WkspMaterialListMapper wkspMaterialListMapper;

    @Autowired
    private WkspMaterialListDetailMapper wkspMaterialListDetailMapper;

    @Autowired
    private LnyStoreroomMapper lnyStoreroomMapper;

    @Autowired
    private LnyProInventoryMapper lnyProInventoryMapper;

    /**
     * 查询出库管理
     * 
     * @param id 出库管理主键
     * @return 出库管理
     */
    @Override
    public LnyStorageOut selectLnyStorageOutById(Long id)
    {
        return lnyStorageOutMapper.selectLnyStorageOutById(id);
    }

    /**
     * 查询出库管理列表
     * 
     * @param lnyStorageOut 出库管理
     * @return 出库管理
     */
    @Override
    public List<LnyStorageOut> selectLnyStorageOutList(LnyStorageOut lnyStorageOut)
    {
        return lnyStorageOutMapper.selectLnyStorageOutList(lnyStorageOut);
    }

    /**
     * 新增出库管理
     * 
     * @param lnyStorageOut 出库管理
     * @return 结果
     */
    @Override
    public int insertLnyStorageOut(LnyStorageOut lnyStorageOut)
    {
        BaseController baseController = new BaseController();
        String username = baseController.getUsername();
        String out = CodeUtils.storageOutCodeCreat();
        Date nowDate = DateUtils.getNowDate();

        lnyStorageOut.setDfsSn(out);
        lnyStorageOut.setCreatedBy(username);
        lnyStorageOut.setCreatedTime(nowDate);
        return lnyStorageOutMapper.insertLnyStorageOut(lnyStorageOut);
    }

    /**
     * 销售出库页面新增入库单
     * @param lnyOutOrder 订单出库联系
     * @return 结果
     */
    public int insertLnyOutOrder(List<LnyOutOrder> lnyOutOrder)
    {
        LnyStorageOut lnyStorageOut = new LnyStorageOut();

        BaseController baseController = new BaseController();
        String username = baseController.getUsername();
        Date nowDate = DateUtils.getNowDate();
        String out = CodeUtils.storageOutCodeCreat();

        //用于获取订单编号建的临时对象
        LnyOutOrder Temp =lnyOutOrder.get(0);

        lnyStorageOut.setDfsSn(out);
        lnyStorageOut.setCreatedBy(username);
        lnyStorageOut.setCreatedTime(nowDate);
        lnyStorageOut.setOrderSn(Temp.getOrderSn());
        lnyStorageOut.setDfsType(0);
        lnyStorageOut.setDfsState(0);

        lnyStorageOutMapper.insertLnyStorageOut(lnyStorageOut);
        for (int i = 0; i < lnyOutOrder.size(); i++)
        {
            LnyOutOrder lnyOutOrderTemp = lnyOutOrder.get(i);
            lnyOutOrderTemp.setOutId(lnyStorageOut.getId());
            lnyOutOrderMapper.insertLnyOutOrder(lnyOutOrderTemp);
        }

        return 1;
    }

    /**
     * 物料出库修改库存数量
     *
     * @param lnyStorageOut 出库管理
     * @return 结果
     */
    public int updateStoreroomFromMaterial(List<LnyStorageOut> lnyStorageOut)
    {
        Long tempId = Long.valueOf(0);
        //物料出库
        for (int i = 0; i < lnyStorageOut.size(); i++)
        {
            LnyStorageOut lnyStorageOutTemp = lnyStorageOut.get(i);
            tempId=lnyStorageOutTemp.getId();
            if(lnyStorageOutTemp.getMatStrmId()!=null){
                //修改库房管理表
                LnyStoreroom lnyStoreroom = new LnyStoreroom();

                lnyStoreroom.setId(lnyStorageOutTemp.getStrmId());
                lnyStoreroom.setNum(lnyStorageOutTemp.getTempNum());
                lnyStoreroomMapper.subLnyStoreroomById(lnyStoreroom);

                //修改领料清单明细表
                WkspMaterialListDetail wkspMaterialListDetail = new WkspMaterialListDetail();
                wkspMaterialListDetail.setMatId(lnyStorageOutTemp.getMatStrmId());
                wkspMaterialListDetail.setSptkSn(lnyStorageOutTemp.getSptkSn());
                wkspMaterialListDetail.setMldId(lnyStorageOutTemp.getMldId());
                wkspMaterialListDetail.setStrmId(lnyStorageOutTemp.getStrmId());
                wkspMaterialListDetail.setTempNum(lnyStorageOutTemp.getTempNum());
                wkspMaterialListDetailMapper.updateLnyRmOrderDetailFromMaterial(wkspMaterialListDetail);

                //修改库存总览表
                LnyProInventory lnyProInventory = new LnyProInventory();

                //根据物料id和库房id UPDATE
                lnyProInventory.setMatId(lnyStorageOutTemp.getMatStrmId());
                lnyProInventory.setStrmId(lnyStorageOutTemp.getStrmId());

                lnyProInventory.setNum(lnyStorageOutTemp.getTempNum());
                lnyProInventoryMapper.subLnyProInventoryByMatId(lnyProInventory);

            }

        }
        LnyStorageOut Temp =new LnyStorageOut();
        Temp.setDfsDate(DateUtils.getNowDate());
        Temp.setId(tempId);
        lnyStorageOutMapper.updateTimeById(Temp);
        return 1;
    }

    /**
     * 采购出库修改库存数量
     *
     * @param lnyOutOrder 订单出库联系
     * @return 结果
     */
    public int updateStoreroomFromOrder(List<LnyOutOrder> lnyOutOrder)
    {
        Long tempId = Long.valueOf(0);
        //采购出库
        for(int i = 0; i < lnyOutOrder.size(); i++)
        {
            LnyOutOrder lnyOutOrderTemp = lnyOutOrder.get(i);
            tempId=lnyOutOrderTemp.getOutId();
            if(lnyOutOrderTemp.getProId()!=null){
                //修改库房管理表
                LnyStoreroom lnyStoreroom = new LnyStoreroom();

                lnyStoreroom.setId(lnyOutOrderTemp.getStrmId());
                lnyStoreroom.setNum(lnyOutOrderTemp.getTempNum());
                lnyStoreroomMapper.subLnyStoreroomById(lnyStoreroom);

                //修改订单出库联系表

                lnyOutOrderMapper.updateLnyOutOrderFromOrder(lnyOutOrderTemp);

                //修改库存总览表
                LnyProInventory lnyProInventory = new LnyProInventory();

                //根据产品id和库房id UPDATE
                lnyProInventory.setProId(lnyOutOrderTemp.getProId());
                lnyProInventory.setStrmId(lnyOutOrderTemp.getStrmId());

                lnyProInventory.setNum(lnyOutOrderTemp.getTempNum());
                lnyProInventoryMapper.subLnyProInventoryByProId(lnyProInventory);
            }

        }
        LnyStorageOut Temp =new LnyStorageOut();
        Temp.setDfsDate(DateUtils.getNowDate());
        Temp.setId(tempId);
        lnyStorageOutMapper.updateTimeById(Temp);
        return 1;
    }

    /**
     * 领料清单新增领料清单及出库管理
     *
     * @param wkspMaterialListDetail 出库管理
     * @return 结果
     */
    @Override
    public int pickingInsertLnyStorageOut(List<WkspMaterialListDetail> wkspMaterialListDetail)
    {
        BaseController baseController = new BaseController();
        String username = baseController.getUsername();
        String out = CodeUtils.storageOutCodeCreat();
        Date nowDate = DateUtils.getNowDate();
        String x = "";      //存车间任务编号


        //新增领料清单表，以获取领料清单id
        WkspMaterialList wkspMaterialList = new WkspMaterialList();
        wkspMaterialList.setCreatedBy(username);
        wkspMaterialList.setCreatedTime(nowDate);
        wkspMaterialListMapper.insertWkspMaterialList(wkspMaterialList);

        //新增领料清单明细表
        for (int i = 0; i < wkspMaterialListDetail.size(); i++){
            WkspMaterialListDetail wkspMaterialListDetailTemp = wkspMaterialListDetail.get(i);

            wkspMaterialListDetailTemp.setMldId(wkspMaterialList.getId());
            x=wkspMaterialListDetailTemp.getSptkSn();
            wkspMaterialListDetailMapper.insertWkspMaterialListDetail(wkspMaterialListDetailTemp);
        }

            //插入出库管理表
        LnyStorageOut lnyStorageOut = new LnyStorageOut();
        lnyStorageOut.setDfsSn(out);
        lnyStorageOut.setMldId(wkspMaterialList.getId());
        lnyStorageOut.setSptkSn(x);
        lnyStorageOut.setDfsType(1);
        lnyStorageOut.setUserId(username);
        lnyStorageOut.setCreatedBy(username);
        lnyStorageOut.setCreatedTime(nowDate);
        lnyStorageOutMapper.insertLnyStorageOut(lnyStorageOut);
        return 1;
    }

    /**
     * 销售出库查询物料相关的接口
     *
     * @param id 出库单id
     * @return 结果
     */
    public List<LnyOutOrder> selectFromMaterialOutByOutId(Long id)
    {
        return lnyOutOrderMapper.selectLnyOutOrderListByOutId(id);
    }

    /**
     * 修改出库管理
     * 
     * @param lnyStorageOut 出库管理
     * @return 结果
     */
    @Override
    public int updateLnyStorageOut(LnyStorageOut lnyStorageOut)
    {
        return lnyStorageOutMapper.updateLnyStorageOut(lnyStorageOut);
    }

    /**
     * 批量删除出库管理
     * 
     * @param ids 需要删除的出库管理主键
     * @return 结果
     */
    @Override
    public int deleteLnyStorageOutByIds(Long[] ids)
    {
        return lnyStorageOutMapper.deleteLnyStorageOutByIds(ids);
    }

    /**
     * 删除出库管理信息
     * 
     * @param id 出库管理主键
     * @return 结果
     */
    @Override
    public int deleteLnyStorageOutById(Long id)
    {
        return lnyStorageOutMapper.deleteLnyStorageOutById(id);
    }
}
